package com.lazy.book.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.lazy.book.common.entity.ApiResult;
import com.lazy.book.common.pojp.LoginInfo;
import com.lazy.book.common.pojp.LoginVO;
import com.lazy.book.service.system.AuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
@Slf4j
public class AuthController {

  private final AuthService authService;


  @SaIgnore
  @PostMapping("login")
  public ApiResult<LoginVO> login(@RequestBody LoginInfo loginInfo) {
    return ApiResult.success(authService.loginClient(loginInfo));
  }

  @PostMapping("logout")
  public ApiResult<Void> logout() {
    // 注意执行顺序，最后再执行logout
    StpUtil.getTokenSession().logout(); // 清除缓存session
    StpUtil.logout(); // 退出登录
    return ApiResult.success();
  }
}
